package com.apache.passport.common;

import com.apache.cache.service.impl.LoadCacheFactory;
import com.apache.cache.util.Validator;
import com.apache.database.datasource.ConnctionData;
import com.apache.database.datasource.DynamicDataSource;
import com.apache.rpc.entity.RpcDatasource;
import com.apache.tools.ClassToolsUtil;
import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * description: 转换模型工厂
 *
 * @author dyh 创建时间：2019年03月03日
 */
public class ConversionModelFactory {

    private static ConversionModelFactory instance = new ConversionModelFactory();

    private SsoConversionModel model;
    private boolean isEnable =  false;
    private String jdbcUrl;
    private String jdbcDeriver;
    private String jdbcUser;
    private String jdbcPwd;
    public static final String USER_DB_KEY="userDb";

    private ConversionModelFactory(){

    }

    public static ConversionModelFactory getInstance(){
        return instance;
    }

    public String readModelXMl(){
        String filePath = Validator.getClassLoaderPath()+"sso-model.xml";
        FileInputStream inStream= null;
        BufferedReader in = null;
        StringBuilder xmlStr= new StringBuilder();
        int len = 0;
        File xmlfile = new File(filePath);
        try{
            inStream= new FileInputStream(xmlfile);
            in= new BufferedReader(new InputStreamReader(inStream,"UTF-8"));
            String line = null;
            while( (line=in.readLine())!=null ){
                if(len != 0) {// 处理换行符的问题
                    xmlStr.append("\r\n"+line);
                } else {
                    xmlStr.append(line);
                }
                len++;
            }
        }catch(Exception e){

        }finally{
            if(in != null){
                IOUtils.closeQuietly(in);
            }
            if(inStream != null){
                IOUtils.closeQuietly(inStream);
            }
        }
        return xmlStr.toString();
    }

    public void setModel(SsoConversionModel model) {
        this.model = model;
        if(Validator.isNotNull(model.getEnableConfig())) {
            this.isEnable = Boolean.parseBoolean(model.getEnableConfig());
        }
        if(this.isEnable) {
            this.jdbcDeriver = model.getJdbcDeriver();
            this.jdbcUrl = model.getJdbcUrl();
            this.jdbcUser = model.getJdbcUser();
            this.jdbcPwd = model.getJdbcPwd();
            RpcDatasource rds = new RpcDatasource();
            rds.setSysEname(USER_DB_KEY);
            rds.setDataName(USER_DB_KEY);
            rds.setDriverClassName(jdbcDeriver);
            rds.setJdbcUrl(jdbcUrl);
            rds.setJdbcUsername(jdbcUser);
            rds.setJdbcPassword(jdbcPwd);
            rds.setDatabaseType(ConnctionData.getInstance().getDataBaseType(null,jdbcDeriver,null));
            LoadCacheFactory
                    .getInstance().getCacheManager("").createCacheObject("rpc_datasource_"+rds.getSysEname(),rds);
            /*if (!DynamicDataSource.getInstance().checkDataSource(USER_DB_KEY)) {
                DynamicDataSource.getInstance()
                        .addDataSource(USER_DB_KEY, jdbcDeriver, jdbcUrl, jdbcUser, jdbcPwd);
            }*/
        }
    }

    public SsoConversionModel getModel() {
        return model;
    }

    public boolean isEnable() {
        return isEnable;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public String getJdbcDeriver() {
        return jdbcDeriver;
    }

    public String getJdbcUser() {
        return jdbcUser;
    }

    public String getJdbcPwd() {
        return jdbcPwd;
    }
}
